<!-- 选择项目订单 -->
<template>
  <el-dialog
    v-model="dialogFormVisible"
    title="选择项目订单"
    width="70%"
    draggable
    overflow
    @close="handleClose"
  >
    <div class="crossRope" />
    <vxe-table
      ref="xTable"
      border
      min-height="350"
      :column-config="{ resizable: true }"
      :row-config="{ isHover: true }"
      :data="tableData"
      size="mini"
      :loading="tableLoading"
      :pager-config="tablePage"
      style="margin-top: 2%"
    >
      <vxe-column type="checkbox" width="60" align="center" fixed="left" />
      <vxe-column
        v-for="column in tableColumns"
        :key="column.field"
        header-align="center"
        :field="column.field"
        :title="column.title"
        :min-width="column.width"
        :fixed="column.fixed"
        :align="column.align"
        show-overflow
      >
        <template v-if="column.field === 'priceType'" v-slot:default="{ row }">
          <span v-if="row.priceType == '0'">锁定</span>
          <span v-if="row.priceType == '1'">浮动</span>
        </template>
        <template v-else v-slot:default="{ row }">
          {{ row[column.field] }}
        </template>
      </vxe-column>
      <vxe-column title="操作" min-width="100" align="center" fixed="right">
        <template v-slot:default="{ row }">
          <vxe-button
            type="text"
            size="small"
            @click="
              toDetail(
                { id: row.id, title: row.orderName, action: 'view' },
                'viewProjectOrder'
              )
            "
            >查看</vxe-button
          >
        </template>
      </vxe-column>
    </vxe-table>
    <vxe-pager
      v-model:current-page="tablePage.currentPage"
      v-model:page-size="tablePage.pageSize"
      :layouts="[
        'Sizes',
        'PrevJump',
        'PrevPage',
        'Number',
        'NextPage',
        'NextJump',
        'FullJump',
        'Total'
      ]"
      :total="tablePage.total"
      size="mini"
      style="margin-top: 2%"
      @page-change="handlePageChange"
    />
    <div class="crossRope" />
    <div style="text-align: right; margin-top: 2%">
      <el-button
        color="#1E3485"
        :icon="Select"
        size="small"
        @click="selectConfirm"
        >确定</el-button
      >
      <el-button :icon="CloseBold" size="small" @click="handleClose"
        >关闭</el-button
      >
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from "vue";
import { ElDialog, ElMessage } from "element-plus";
import { Select, CloseBold } from "@element-plus/icons-vue";
import { VxePagerEvents } from "vxe-table";
import { useDetail } from "@/views/ERP_OrderManage/ERP_ProjectOrder/components/utils/hooks";
import { getprojectOrderlist } from "@/api/ERP_OrderManage";

// 路由跳转
const { toDetail, router } = useDetail();

// 弹窗关闭/打开事件
const props = defineProps({
  visible: Boolean,
  contractId: String
});
const dialogFormVisible = ref(false);
watch(
  () => props.visible,
  val => {
    dialogFormVisible.value = val;
  },
  { immediate: true }
);
const emit = defineEmits(["close", "confirm"]);
function handleClose() {
  emit("close", false);
}

// 表格配置
let tableLoading = ref(false);
let tableData = ref([]);
let xTable = ref(null);
const tableColumns = reactive([
  {
    field: "orderSn",
    title: "订单编号",
    width: 150,
    align: "left",
    fixed: "left"
  },
  {
    field: "orderName",
    title: "订单名称",
    width: 150,
    align: "left",
    fixed: "left"
  },
  {
    field: "supplierName",
    title: "供应商名称",
    width: 150,
    align: "left"
  },
  {
    field: "taxTotalPrice",
    title: "含税总价（元）",
    width: 150,
    align: "right"
  },
  {
    field: "priceType",
    title: "价格类型",
    width: 150,
    align: "center"
  },
  {
    field: "receivingTime",
    title: "计划收货时间",
    width: 150,
    align: "center"
  },
  {
    field: "flowSubmitDate",
    title: "订单下单时间",
    width: 150,
    align: "center"
  },
  {
    field: "supplyConfirmDate",
    title: "订单确认时间",
    width: 150,
    align: "center"
  }
]);
// 表格分页配置
const tablePage = reactive({
  total: 0,
  currentPage: 1,
  pageSize: 10
});
// 表格分页事件
const handlePageChange: VxePagerEvents.PageChange = ({
  currentPage,
  pageSize
}) => {
  tablePage.currentPage = currentPage;
  tablePage.pageSize = pageSize;
  getList();
};

// 获取表格数据
async function getList() {
  let params: ApiParams = {};
  params.pageIndex = tablePage.currentPage;
  params.pageSize = tablePage.pageSize;
  params.contractId = props.contractId;
  params.supplyStatus = 3;
  params.projectInvoiceStatus = 0;
  const response: ApiResponse = await getprojectOrderlist(params);
  if (response.code === 200) {
    tableData.value = response.data.records;
    tablePage.total = response.data.totalElements;
  } else {
    ElMessage.error("获取项目订单列表失败" + response.msg);
  }
}
// 获取选中行
function getSelectedRows() {
  return xTable.value.getCheckboxRecords();
}
// 确认事件
async function selectConfirm() {
  let selectItem = getSelectedRows();
  if (selectItem.length === 0) {
    ElMessage.error("请选择一条数据");
  } else {
    let dataList = [];
    for (let i = 0; i < selectItem.length; i++) {
      dataList.push({
        id: selectItem[i].id,
        orderSn: selectItem[i].orderSn,
        orderName: selectItem[i].orderName,
        supplierName: selectItem[i].supplierName,
        taxTotalPrice: selectItem[i].taxTotalPrice,
        priceType: selectItem[i].priceType,
        receivingTime: selectItem[i].receivingTime,
        flowSubmitDate: selectItem[i].flowSubmitDate,
        supplyConfirmDate: selectItem[i].supplyConfirmDate
      });
    }
    emit("confirm", {
      dataList: dataList
    });
    // 关闭弹窗
    handleClose();
  }
}

// 打开弹窗时执行
watch(dialogFormVisible, newValue => {
  if (newValue) {
    getList();
  }
});
</script>

<style lang="scss" scoped></style>
